package com.wantong.jianpu.activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.view.View;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.lxj.xpopup.XPopup;
import com.wantong.jianpu.BR;
import com.wantong.jianpu.R;
import com.wantong.jianpu.base.BaseXActivity;
import com.wantong.jianpu.beans.SelectImageBean;
import com.wantong.jianpu.customviews.SelectImageView;
import com.wantong.jianpu.databinding.ActivityFaultCreateBinding;
import com.wantong.jianpu.dialogs.LocationPopupView;
import com.wantong.jianpu.viewmodels.FaultCreateViewModel;

import java.util.Calendar;
import java.util.List;

import me.goldze.mvvmhabit.utils.DateUtil;

/**
 * @author PACY
 * @date 2022/3/23
 * description: 故障上报
 */
public class FaultCreateActivity extends BaseXActivity<ActivityFaultCreateBinding, FaultCreateViewModel> {
    TimePickerView popup;

    private LocationPopupView locationPopupView;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_fault_create;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        setToolbar(binding.baseToolBar);
        viewModel.isCreate=getIntent().getExtras().getBoolean("isCreate");
        if(getIntent().getExtras().getString("id")!=null){
            viewModel.id=getIntent().getExtras().getString("id");
        }
        binding.selectImageView.setActivity(this);
        binding.selectImageView.setCallBack(new SelectImageView.SelectCallBack() {
            @Override
            public void selectImagePath(List<String> filePath) {
                viewModel.upLoadFile(filePath);
            }

            @Override
            public void deletePosition(int position) {
                viewModel.commitEvent.getValue().getFaultFiles().remove(position);
            }
        });

        viewModel.image.observe(this, selectImageBeans -> {
            binding.selectImageView.setData(selectImageBeans);
        });
        binding.checkTimeCsv.setOnClickListener(v -> {
            showDatePicker();
        });

        viewModel.deviceEvent.observe(this, strings -> {
            new XPopup.Builder(FaultCreateActivity.this)
                    .asBottomList("请选择设备", strings,
                            (position, text) -> {
                                viewModel.setDevicePosition(position);
                            }).show();
        });

        binding.locationCsv.setOnClickListener(v -> {
            showLocationPopup();
        });
    }

    /**
     * 位置信息弹窗
     */
    private void showLocationPopup(){
        if(viewModel.locationBeans==null){
            return;
        }
        if(locationPopupView==null){
            locationPopupView=new LocationPopupView(FaultCreateActivity.this,viewModel.locationBeans);
            locationPopupView.setCallBack(location -> {
                viewModel.location.setValue(location.getName());
                viewModel.commitEvent.getValue().setLocationId(location.getId());
            });
        }
        new XPopup.Builder(FaultCreateActivity.this)
                .asCustom(locationPopupView)
                .show();
    }

    /**
     * 日期选择器
     */
    private void showDatePicker() {
        Calendar cal = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal.set(cal2.get(Calendar.YEAR) - 5, 0, 1);

        if (popup == null) {
            popup = new TimePickerBuilder(FaultCreateActivity.this, (date, v) -> {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                String month = (calendar.get(Calendar.MONTH) + 1) + "";
                month = month.length() > 1 ? month : "0" + month;
                viewModel.commitEvent.getValue().setDaultDate(calendar.get(Calendar.YEAR) + "-" + month+"-"+calendar.get(Calendar.DAY_OF_MONTH)+" "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND));
                viewModel.commitEvent.setValue(viewModel.commitEvent.getValue());
            })
                    .setType(new boolean[]{true, true, true, true, true, true})
                    .setTitleText("选择时间")
                    .setRangDate(cal, cal2)
                    .build();
        }
        popup.setDate(Calendar.getInstance());
        popup.show();

    }
}